<?php

namespace app\common\model\banxi\core;

use app\common\enums\core\AuditStateEnum;
use app\common\enums\TimeApplyTypeEnum;
use app\common\model\banxi\trainer\Trainer;
use app\common\model\common\BaseModel;
use think\model\concern\SoftDelete;

/**
 * 请假
 */
class CoreTrainerTimeApply extends BaseModel
{

    use SoftDelete;

    protected $name = 'banxi_core_trainer_time_apply';

    protected $autoWriteTimestamp = true;

    protected $deleteTime = 'del';

    protected $defaultSoftDelete = 0;

    protected $append = [
        'applyTypeCn',
        'auditStateCn'
    ];

    protected $type = [
        'id' => 'string',
        'del' => 'string',
        'trainerId' => 'string',
        'createTime' => 'timestamp:Y-m-d H:i:s',
        'updateTime' => 'timestamp:Y-m-d H:i:s',
    ];

    //-----------------------------------------------------------------------------------
    //｜                                   获取器                                       ｜
    //-----------------------------------------------------------------------------------

    public function getAuditStateCnAttr($value, $data)
    {
        $value = $value ?: ($data['auditState'] ?? '');
        return (new AuditStateEnum())->getDescription($value) ?? '';
    }

    public function getApplyTypeCnAttr($value, $data)
    {
        $value = $value ?: ($data['applyType'] ?? '');
        return (new TimeApplyTypeEnum())->getDescription($value) ?? '';

    }

    //-----------------------------------------------------------------------------------
    //｜                                   处理方法                                       ｜
    //-----------------------------------------------------------------------------------
    public function getListPage(array $where, string $page = "1", int $limit = 10)
    {
        return $this->with([
            'trainer' => function ($query) {
                $query->field('id,name');
            }
        ])->where($where)
            ->order(['id' => 'desc'])
            ->paginate([
                'page' => $page,
                'list_rows' => $limit,
            ]);
    }

    public function getInfo(array $where)
    {
        return $this->with([
            'trainer' => function ($query) {
                $query->field('id,name');
            }
        ])->where($where)
            ->find();
    }

    public function del(array $ids)
    {
        if (empty($ids)) {
            return true;
        }
        $where = [['id', 'in', $ids]];
        return self::destroy(function ($query) use ($where) {
            $query->where($where);
        });
    }
    //-----------------------------------------------------------------------------------
    //｜                                   模型关联                                       ｜
    //-----------------------------------------------------------------------------------
    public function trainer()
    {
        return $this->hasOne(Trainer::class, 'id', 'trainerId');
    }
}